home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / mint / duftp / servers.c < prev    next >
C/C++ Source or Header  |  1995-06-17  |  8KB  |  302 lines

  1. /*
  2.     DUFTP
  3. */
  4.  
  5. // Server List Handling
  6.  
  7. #include <DULIB.H>
  8. #include <stdio.h>
  9. #include "servers.h"
  10. #include "duftp_n.h"
  11. #include "globals.h"
  12. #include "ftp.h"
  13.  
  14. server_list *servers;
  15. server_list *selected_server;
  16. short server_count;
  17. char *server_name_list[MAX_SERVERS];
  18.  
  19. #define MAX_STD_LENGTH 40
  20.  
  21. // default root account on my own machine, this sets the password to popwilleat, so you have to change it to your own machine
  22. // or even better, don't use it and make sure the bookmarks file SERVERS.INF exists
  23.  
  24. const server_list servers_default = {"Localhost (this machine)             ","localhost","root","popwilleat                  ","/"};
  25.  
  26. void load_servers(void)
  27. {
  28.     FILE *s;
  29.     char linebuf[200];
  30.     server_list *ns;
  31.     
  32.     s=fopen("SERVERS.INF","r");            // Check for both cases of the servers.inf file
  33.     if (!s)                                // (request by Ben Ball)
  34.         s=fopen("servers.inf","r");
  35.     
  36.     if (s)
  37.     {
  38.         servers=NULL;
  39.         server_count=0;
  40.         
  41.         fgets(linebuf,199,s);        // Skip title line
  42.         while(!feof(s))
  43.         {
  44.             ns=(server_list*)malloc(sizeof(server_list));
  45.             fgets(linebuf,199,s);
  46.             linebuf[strlen(linebuf)-1]='\0';
  47.             ns->name=(char*)malloc(sizeof(char)*MAX_STD_LENGTH);
  48.             sprintf(ns->name,"%s",linebuf);
  49.             fgets(linebuf,199,s);
  50.             linebuf[strlen(linebuf)-1]='\0';
  51.             ns->ip_name=(char*)malloc(sizeof(char)*MAX_STD_LENGTH);
  52.             sprintf(ns->ip_name,"%s",linebuf);
  53.             fgets(linebuf,199,s);
  54.             linebuf[strlen(linebuf)-1]='\0';
  55.             ns->login=(char*)malloc(sizeof(char)*MAX_STD_LENGTH);
  56.             sprintf(ns->login,"%s",linebuf);
  57.             fgets(linebuf,199,s);
  58.             linebuf[strlen(linebuf)-1]='\0';
  59.             ns->password=(char*)malloc(sizeof(char)*MAX_STD_LENGTH);
  60.             sprintf(ns->password,"%s",linebuf);
  61.             ns->next=servers;
  62.             fgets(linebuf,199,s);
  63.             linebuf[strlen(linebuf)-1]='\0';
  64.             ns->path=(char*)malloc(sizeof(char)*MAX_STD_LENGTH);
  65.             sprintf(ns->path,"%s",linebuf);
  66.             ns->next=servers;
  67.             servers=ns;
  68.             server_name_list[server_count]=ns->name;
  69.             server_count++;
  70.         }
  71.         fclose(s);
  72.         for(selected_server=servers; selected_server->next; selected_server=selected_server->next);
  73.     }else{
  74.         selected_server=servers=&servers_default;          // Use the internal default
  75.         server_name_list[0]=servers->name;
  76.         server_count=1;
  77.         form_alert(1,"[1][ WARNING: Cann't find yours SERVERS.INF file. | Using internal default. ][ ok ]");
  78.     }
  79. }
  80.  
  81. short save_servers(void)
  82. {
  83.     FILE *s;
  84.     server_list *ns;
  85.  
  86.     if (servers==&servers_default) return TRUE;        // Don't save the internal default
  87.  
  88.     chdir(initial_dir);
  89.     s=fopen("SERVERS.INF","w");
  90.  
  91.     if (s)
  92.     {
  93.         fprintf(s,"DUFTP SERVERS FILE");
  94.         for(ns=servers; ns; ns=ns->next)
  95.         {
  96.             fprintf(s,"\n%s\n%s\n%s\n%s\n%s",ns->name,ns->ip_name,ns->login,ns->password,ns->path);
  97.         }
  98.         fprintf(s,"*");
  99.         fclose(s);
  100.     }else{
  101.         form_alert(1,"[3][ WARNING: Cann't open SERVERS.INF file. ][ ok ]");
  102.     }
  103.  
  104.     return TRUE;
  105. }
  106.  
  107. short add_new_server(void)
  108. {
  109.     OBJECT *a;
  110.     server_list *ns;
  111.     short f;
  112.     
  113.     ns=(server_list*)malloc(sizeof(server_list));
  114.     
  115.     ns->password=(char*)malloc(sizeof(char)*MAX_STD_LENGTH);
  116.     ns->path=(char*)malloc(sizeof(char)*MAX_STD_LENGTH);
  117.     ns->name=(char*)malloc(sizeof(char)*MAX_STD_LENGTH);
  118.     ns->login=(char*)malloc(sizeof(char)*MAX_STD_LENGTH);
  119.     ns->ip_name=(char*)malloc(sizeof(char)*MAX_STD_LENGTH);
  120.     for(f=0; f<MAX_STD_LENGTH; f++)
  121.     {
  122.         ns->password[f]=' ';
  123.         ns->path[f]=' ';
  124.         ns->name[f]=' ';
  125.         ns->login[f]=' ';
  126.         ns->ip_name[f]=' ';
  127.     }
  128.     ns->password[MAX_STD_LENGTH-1]='\0';
  129.     ns->path[MAX_STD_LENGTH-1]='\0';
  130.     ns->name[MAX_STD_LENGTH-1]='\0';
  131.     ns->login[MAX_STD_LENGTH-1]='\0';
  132.     ns->ip_name[MAX_STD_LENGTH-1]='\0';
  133.     sprintf(ns->name,"NEW                                   ");
  134.  
  135.     if (ns==&servers_default)
  136.         ns->next=NULL;
  137.     else
  138.         ns->next=servers;
  139.  
  140.     servers=ns;
  141.     server_name_list[server_count]=ns->name;
  142.     server_count++;
  143.  
  144. // Update the list of servers to include our new server
  145.     Change_scroll_list(Hosts, Listhosts, server_name_list, server_count);
  146.  
  147. // Make the current editable texts our new ones
  148.     set_dialog_text(Hosts, Server_name, ns->name);
  149.     set_dialog_text(Hosts, Ip_address, ns->ip_name);
  150.     set_dialog_text(Hosts, Login_name, ns->login);
  151.     set_dialog_text(Hosts, Login_password, ns->password);
  152.     set_dialog_text(Hosts, Login_path, ns->path);
  153.     rsrc_gaddr(0,Hosts,&a);
  154.     objc_draw(a,Server_name,0,scrn_x,scrn_y,scrn_w,scrn_h);
  155.     objc_draw(a,Ip_address,0,scrn_x,scrn_y,scrn_w,scrn_h);
  156.     objc_draw(a,Login_name,0,scrn_x,scrn_y,scrn_w,scrn_h);
  157.     objc_draw(a,Login_password,0,scrn_x,scrn_y,scrn_w,scrn_h);
  158.     objc_draw(a,Login_path,0,scrn_x,scrn_y,scrn_w,scrn_h);
  159.  
  160.     return TRUE;
  161. }
  162.  
  163. short add_current_server(void)
  164. {
  165.     server_list *ns;
  166.     short mb,junk;
  167.     
  168.     if (!connected) form_alert(1,"[1][ Cann't add bookmark | - not connected. ][ Cancel ]");
  169.     
  170.     ns=(server_list*)malloc(sizeof(server_list));
  171.     
  172.     ns->password=(char*)malloc(sizeof(char)*MAX_STD_LENGTH);
  173.     ns->name=(char*)malloc(sizeof(char)*MAX_STD_LENGTH);
  174.     ns->login=(char*)malloc(sizeof(char)*MAX_STD_LENGTH);
  175.     ns->ip_name=(char*)malloc(sizeof(char)*MAX_STD_LENGTH);
  176.     ns->path=(char*)malloc(sizeof(char)*MAX_STD_LENGTH);
  177.     
  178.     sprintf(ns->name,"New Bookmark                       ");
  179.     sprintf(ns->login, "%s", new_server_login);
  180.     sprintf(ns->ip_name, "%s", new_server_address);
  181.     sprintf(ns->password, "%s", new_server_password);
  182.     sprintf(ns->path, "%s/", new_server_path);
  183.  
  184.     ns->next=servers;
  185.     servers=ns;
  186.     server_name_list[server_count]=ns->name;
  187.     server_count++;
  188.  
  189. // Update the list of servers to include our new server
  190.     Change_scroll_list(Hosts, Listhosts, server_name_list, server_count);
  191.  
  192. // Make the current editable texts our new ones
  193.     set_dialog_text(Hosts, Server_name, ns->name);
  194.     set_dialog_text(Hosts, Ip_address, ns->ip_name);
  195.     set_dialog_text(Hosts, Login_name, ns->login);
  196.     set_dialog_text(Hosts, Login_password, ns->password);
  197.     set_dialog_text(Hosts, Login_path, ns->path);
  198.     Set_dialog_Kfocus(Hosts, Server_name);
  199.  
  200.     dialog_update(Hosts);
  201.  
  202.     for(mb=1; mb; graf_mkstate(&junk,&junk,&mb,&junk));
  203.  
  204.     return TRUE;
  205. }
  206.  
  207. short popup_select_server(void)
  208. {
  209.     char **list;
  210.     short f,s,current;
  211.     server_list *l;
  212.     
  213.     l=servers;
  214.     
  215.     list=(char**)malloc(sizeof(char*)*(server_count+2));
  216.     
  217.     for(f=server_count; f>0; f--)
  218.     {
  219.         list[f]=(char*)malloc(sizeof(char)*(strlen(l->name)+2));
  220.         sprintf(list[f],"%s",l->name);
  221.         if (selected_server==l) current=f;
  222.         l=l->next;
  223.     }
  224.     list[0]="SELECT FTP SITE";
  225.     
  226.     s=form_popup(this_dialog, this_ob, server_count, current, list);
  227.     
  228.     s=server_count-s;
  229.     selected_server=servers;
  230.     for(f=0; f<s; f++) selected_server=selected_server->next;
  231.  
  232.     set_dialog_text(this_dialog,this_ob,selected_server->name);
  233.     
  234.     for(f=0; f<server_count; f++) free(list[f]);
  235.     free(list);
  236.     return TRUE;
  237. }
  238.  
  239. short delete_server(void)
  240. {
  241.     server_list *l,*ll;
  242.     short f, current;
  243.  
  244.     if (selected_server==&servers_default) return TRUE;
  245.  
  246.     l=servers; ll=NULL; current=-1;
  247.     for(f=server_count-1; (f>-1)&&(current==-1); f--)
  248.         if (selected_server==l)
  249.         {
  250.             current=f;
  251.         }else{
  252.             ll=l;
  253.             l=l->next;
  254.         }
  255.     
  256.     if (current==-1) return TRUE;
  257.  
  258.     for(f=current; f<server_count; f++)
  259.         server_name_list[f]=server_name_list[f+1];
  260.  
  261.     if (ll)                    // Unhitch the deleted element from the servers list
  262.         ll->next=l->next;
  263.     else
  264.         servers=l->next;
  265.     
  266.     free(l->name);            // dispose of the server entry
  267.     free(l->ip_name);
  268.     free(l->login);
  269.     free(l->password);
  270.     free(l->path);
  271.     free(l);
  272.     
  273.     server_count--;
  274.     
  275.     if (servers==NULL)        // prevent a totally empty list
  276.     {
  277.         servers=&servers_default;
  278.         server_name_list[0]=servers->name;
  279.         server_count=1;
  280.     }
  281.  
  282. // Update the list of servers
  283.     Change_scroll_list(Hosts, Listhosts, server_name_list, server_count);
  284.  
  285. // Make the current editable texts our new ones
  286.     for(l=servers; l->next; l=l->next);
  287.     set_dialog_text(Hosts, Server_name, l->name);
  288.     set_dialog_text(Hosts, Ip_address, l->ip_name);
  289.     set_dialog_text(Hosts, Login_name, l->login);
  290.     set_dialog_text(Hosts, Login_password, l->password);
  291.     set_dialog_text(Hosts, Login_path, l->path);
  292.     Set_dialog_Kfocus(Hosts, Server_name);
  293.     set_dialog_text(Mainwindow, This_server, l->name);
  294.     selected_server=l;
  295.     
  296.     dialog_update(Hosts);
  297.     dialog_update(Mainwindow);
  298.  
  299.     for(f=1; f; graf_mkstate(¤t,¤t,&f,¤t));
  300.     
  301.     return TRUE;
  302. }